package com.caldera.vaquita;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

public class VaquitaCajaFm extends Activity{
	VaquitaUtils utils;
	VaquitaData data;
	SQLiteDatabase db;
	Cursor rs;
	String sql;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cajafm);
        
        data=new VaquitaData(this);
        db=data.getReadableDatabase();
        sql="select _id, nombre from grupo;";
        rs=db.rawQuery(sql, null);
        List<String> grupos=new ArrayList<String>();
        while(rs.moveToNext()){
        	grupos.add(rs.getString(1));
        }
        final Spinner spinner1 = (Spinner) findViewById(R.id.cajaFmGrupo);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, grupos);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);
    }
    public void onAgregar(View v){
    	utils=new VaquitaUtils(this);
    	if(!utils.isStarted(utils.getFecha("yyyy-MM"))){
    		utils.toStart();
    	}
    	int grupo=(int)((Spinner)findViewById(R.id.cajaFmGrupo)).getSelectedItemId()+1;
    	String detalle=((EditText)findViewById(R.id.cajaFmDescripcion)).getText().toString();
    	double monto=Double.parseDouble( ((EditText)findViewById(R.id.cajaFmMonto)).getText().toString() );
    	
    	RadioButton rb_out=(RadioButton)findViewById(R.id.RBOptionOut);
    	RadioButton rb_in=(RadioButton)findViewById(R.id.RBOptionIn);
    	if(rb_out.isChecked() == true){
    		utils.toInsert(grupo, detalle, 0.00, monto);
    	}
		if(rb_in.isChecked() == true){
			utils.toInsert(grupo, detalle, monto, 0.00);
		}
    	
		startActivity(new Intent(this, VaquitaActivity.class));
    }
}
